<template>
  <div class="login paga-data">
    <canvas id="canvas" class="canvas"></canvas>
    <div id="chart" class="gl-chart"></div>
    <!-- <div class="login-bg"></div> -->
    <div class="login-form">
      <div class="system-name">
        <img src="../../assets/images/login_title.png" alt="">
      </div>
      <div class="login-content">
        <h4 class="title">成都倍豪物业发展有限公司</h4>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
          <el-form-item label="" prop="usercode">
            <el-input v-model="ruleForm.usercode" placeholder="请输入账号" @keyup.enter.native="submitForm">
              <i slot="prefix" class="el-input__icon el-icon-user"></i>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input v-model="ruleForm.password" placeholder="请输入密码" type="password" @keyup.enter.native="submitForm">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            </el-input>
          </el-form-item>
          <el-form-item class="login-item">
            <el-button type="primary" icon="el-icon-right" @click="submitForm()" :loading="loading">登录</el-button>
            <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  components: {

  },
  data() {
    return {
      loading: false,
      ruleForm: {
        usercode: '',
        password: '',
      },
      rules: {
        usercode: [
          { required: true, message: '请输入账号', trigger: 'submit' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'submit' },
        ],
      },
      showEarth: false,
    };
  },
  mounted() {
    // this.$root.userInfo={};
    this.glEarth()
    this.canvas();
  },
  methods: {
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          let params = this.ruleForm;
          this.loading = true;
          setTimeout(() => {
            this.loading = false;
            if (params.usercode == "admin" && (params.password == "bh-123123" || params.password == "bh-61290208")) {
              let userInfo = {
                token: this.func.uuid(),
              }
              this.func.setSession("userInfo", userInfo);
              this.$router.replace({ path: '/monitor' });
              // this.func.message("登录成功", "success");
            } else {
              this.func.message("error", "账号或者密码错误");
            }
          }, 200)

          // this.https("post", "/api/Account/Login", params).then(res => {
          //   this.loading = false;
          //   if (res.state && res.code == 200) {
          //     let userInfo = res.data;
          //     userInfo.token = userInfo.token_type + " " + userInfo.access_token;
          //     this.$root.userInfo = userInfo;
          //     this.func.setSession("userInfo", userInfo);
          //     this.func.message("登录成功", "success");
          //     this.$router.replace({ path: '/home/overview' });
          //     this.$root.getUserBaseInfo();
          //   } else {
          //     this.func.message(res.msg);
          //   }
          // }).catch(() => {
          //   this.loading = false;
          // })
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    },
    canvas() {
      var canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d'),
        w = canvas.width = window.innerWidth / 1.7,
        h = canvas.height = window.innerHeight,

        hue = 0,
        stars = [],
        count = 0,
        maxStars = 600;

      // Thanks @jackrugile for the performance tip! http://codepen.io/jackrugile/pen/BjBGoM
      // Cache gradient
      var canvas2 = document.createElement('canvas'),
        ctx2 = canvas2.getContext('2d');
      canvas2.width = 100;
      canvas2.height = 100;
      var half = canvas2.width / 2,
        gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);
      gradient2.addColorStop(0.1, '#00DAD7');
      gradient2.addColorStop(0.1, 'rgba(255,255,255,0.1)');
      gradient2.addColorStop(0.25, 'rgba(0,0,0,0.05)');
      gradient2.addColorStop(1, 'transparent');

      ctx2.fillStyle = gradient2;
      ctx2.beginPath();
      ctx2.arc(half, half, half, 0, Math.PI * 2);
      ctx2.fill();

      // End cache

      function random(min, max) {
        if (arguments.length < 2) {
          max = min;
          min = 0;
        }

        if (min > max) {
          var hold = max;
          max = min;
          min = hold;
        }

        return Math.floor(Math.random() * (max - min + 1)) + min;
      }

      var Star = function () {

        this.orbitRadius = random(w / 2 - 50);
        this.radius = random(500, this.orbitRadius) / 10;
        this.orbitX = w / 2;
        this.orbitY = h / 2;
        this.timePassed = random(0, maxStars);
        this.speed = random(this.orbitRadius) / 100000;
        this.alpha = random(2, 10) / 10;

        count++;
        stars[count] = this;
      }

      Star.prototype.draw = function () {
        var x = Math.sin(this.timePassed + 1) * this.orbitRadius + this.orbitX,
          y = Math.cos(this.timePassed) * this.orbitRadius / 2 + this.orbitY,
          twinkle = random(10);

        if (twinkle === 1 && this.alpha > 0) {
          this.alpha -= 0.05;
        } else if (twinkle === 2 && this.alpha < 1) {
          this.alpha += 0.05;
        }

        ctx.globalAlpha = this.alpha;
        ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);
        this.timePassed += this.speed;
      }

      for (var i = 0; i < maxStars; i++) {
        new Star();
      }

      function animation() {
        ctx.globalCompositeOperation = 'source-over';
        ctx.globalAlpha = 0.8;
        ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 1)';
        ctx.fillRect(0, 0, w, h)

        ctx.clearRect(0, 0, w, h);

        ctx.globalCompositeOperation = 'lighter';
        for (var i = 1, l = stars.length; i < l; i++) {
          stars[i].draw();
        };

        window.requestAnimationFrame(animation);
      }
      animation();
    },
    glEarth() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById('chart'));
      // 指定图表的配置项和数据
      var option = null;
      var baseTexture = null;
      var geoJson = null;
      // 生成球面纹理
      function getBaseTexture() {
        echarts.registerMap('world', geoJson);
        let canvas = document.createElement('canvas');
        baseTexture = echarts.init(canvas, null, {
          width: 4096,
          height: 2048,
        });
        baseTexture.setOption({
          backgroundColor: '#16244B',
          geo: {
            type: 'map',
            map: 'world',
            left: 0,
            top: 0,
            right: 0,
            bottom: 0,
            boundingCoords: [
              [-180, 90],
              [180, -90],
            ],
            roam: false,
            itemStyle: {
              normal: {
                areaColor: '#2455ad',
                borderColor: '#000c2d',
              },
              emphasis: {
                areaColor: '#357cf8',
              },
            },
            label: {
              normal: {
                fontSize: 20,
                show: false,
                textStyle: {
                  color: '#fff',
                },
              },
            },
          },
        });

        drawEarth();
      }
      // 绘制球体
      function drawEarth() {
        option = {
          backgroundColor: 'rgba(0,0,0,0)',
          tooltip: {
            trigger: 'item',
          },
          globe: {
            baseTexture: baseTexture, // 基础纹理
            globeRadius: 70, // 球面半径
            environment: 'auto',
            shading: 'color',
            light: {
              // 光照阴影
              main: {
                color: '#fff', // 光照颜色
                intensity: 1, // 光照强度
                alpha: 40,
                beta: -30,
              },
              ambient: {
                color: '#fff',
                intensity: 1,
              },
            },
            viewControl: {
              alpha: 30,
              beta: 160,
              autoRotate: true, // 开启自动旋转
              autoRotateAfterStill: 10,
              distance: 240,
            },
          },
          series: [
            {
              name: '',
              type: 'lines3D',
              coordinateSystem: 'globe',
              effect: {
                show: true,
              },
              blendMode: 'source-over',
              lineStyle: {
                width: 1.2,
                color: "#204395"
              },
              data: [],
              silent: false,
            },
          ],
        };
        // 随机数据 i控制线数量
        for (let i = 0; i < 80; i++) {
          option.series[0].data = option.series[0].data.concat(rodamData());
        }
        myChart.clear();
        myChart.setOption(option, true);
      }
      function rodamData() {
        // 模拟数据，构造飞线的起始经纬度
        let longitude = 104.071271;
        let latitude = 30.655358;
        let longitude2 = Math.random() * 360 - 180;
        let latitude2 = Math.random() * 180 - 90;
        return {
          coords: [
            [longitude, latitude],
            [longitude2, latitude2],
          ],
          value: (Math.random() * 3000).toFixed(2),
        };
      }
      this.https("get", "/static/script/echarts/map/geo0.json", {}, "local").then(res => {
        geoJson = res;
        getBaseTexture();
      })
      //=============== end ===============//
      // 使用刚指定的配置项和数据显示图表。
      if (option && typeof option === 'object') {
        myChart.setOption(option);
      }
      window.addEventListener('resize', myChart.resize);
    }
  },
}
</script>
<style lang="scss">
.login .el-form-item {
  margin-bottom: 25px !important;

  .el-form-item__label {
    color: #fff;
  }

  .el-input__inner {
    background-color: rgba($color: #000000, $alpha: 0.2);
    color: #fff;
    height: 40px;
    border-color: #42b8ff;
    background-color: rgba($color: #126db1, $alpha: 0.2);
    padding-left: 40px;

    &::placeholder {
      color: #fff;
    }

    &:focus::placeholder {
      color: #176ea3;
    }
  }

  .el-input__prefix {
    color: #42b8ff;
    left: 10px;
  }

  .el-input__icon {
    line-height: 40px;
    font-size: 20px;
  }
}
</style>
<style lang="scss" scoped>
.login {
  position: relative;
  height: 100vh;
  width: 100vw;
  background-image: url("../../assets/images/login_bg.jpg");
  background-size: 100% 100%;
  overflow: hidden;
  background-color: #141e3f;

  .login-form {
    position: absolute;
    right: 15%;
    width: 420px;
    top: 22%;
    z-index: 10;

    .system-name {
      text-align: center;
      padding: 0 50px;
      margin-bottom: 30px;

      img {
        width: 100%;
      }
    }

    .login-content {
      background-image: url("../../assets/images/login_content_bg.png");
      background-size: 100% 100%;
      padding: 50px;
      height: auto;

      .title {
        text-align: center;
        font-size: 20px;
        color: #42b8ff;
        margin-bottom: 30px;
        font-weight: normal;
      }

      .login-item {
        text-align: center;
        margin-top: 40px;
        margin-bottom: 0 !important;

        .el-button {
          display: block;
          height: 40px;
          font-size: 15px;
          width: 100%;
        }
      }
    }
  }

  .login-bg {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: 2;
    background-image: url("../../assets/images/login_bg2.png");
    background-repeat: no-repeat;
    background-position: 5% 70%;
    background-size: auto 90%;
  }

  .canvas {
    position: absolute;
    left: 0%;
    z-index: 3;
  }

  .gl-chart {
    position: absolute;
    left: 8%;
    top: 0;
    bottom: 0;
    width: 40%;
    z-index: 2;
  }
}

.bg-bubbles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.bg-bubbles li,
.bg-bubbles ol {
  position: absolute;
  list-style: none;
  display: block;
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.15);
  bottom: -160px;
  -webkit-animation: square 25s infinite;
  animation: square 25s infinite;
  -webkit-transition-timing-function: linear;
  transition-timing-function: linear;
}

.bg-bubbles ol {
  padding: 0 !important;
}

.bg-bubbles ol:nth-child(11) {
  left: 10%;
  top: 10%;
  width: 20px;
  height: 20px;
}

.bg-bubbles ol:nth-child(12) {
  left: 20%;
  top: 40%;
  width: 60px;
  height: 60px;
}

.bg-bubbles ol:nth-child(13) {
  left: 65%;
  top: 30%;

  width: 100px;
  height: 60px;
}

.bg-bubbles ol:nth-child(14) {
  left: 70%;
  top: 30%;
  width: 100px;
  height: 150px;
}

.bg-bubbles ol:nth-child(15) {
  left: 50%;
  top: 70%;
  width: 40px;
  height: 60px;
}

.bg-bubbles li:nth-child(1) {
  left: 10%;
}

.bg-bubbles li:nth-child(2) {
  left: 20%;
  width: 80px;
  height: 80px;
  -webkit-animation-delay: 2s;
  animation-delay: 2s;
  -webkit-animation-duration: 17s;
  animation-duration: 17s;
}

.bg-bubbles li:nth-child(3) {
  left: 25%;
  -webkit-animation-delay: 4s;
  animation-delay: 4s;
}

.bg-bubbles li:nth-child(4) {
  left: 40%;
  width: 60px;
  height: 60px;
  -webkit-animation-duration: 22s;
  animation-duration: 22s;
  background-color: rgba(255, 255, 255, 0.25);
}

.bg-bubbles li:nth-child(5) {
  left: 70%;
}

.bg-bubbles li:nth-child(6) {
  left: 80%;
  width: 120px;
  height: 120px;
  -webkit-animation-delay: 3s;
  animation-delay: 3s;
  background-color: rgba(255, 255, 255, 0.2);
}

.bg-bubbles li:nth-child(7) {
  left: 32%;
  width: 160px;
  height: 160px;
  -webkit-animation-delay: 7s;
  animation-delay: 7s;
}

.bg-bubbles li:nth-child(8) {
  left: 55%;
  width: 20px;
  height: 20px;
  -webkit-animation-delay: 15s;
  animation-delay: 15s;
  -webkit-animation-duration: 40s;
  animation-duration: 40s;
}

.bg-bubbles li:nth-child(9) {
  left: 25%;
  width: 10px;
  height: 10px;
  -webkit-animation-delay: 2s;
  animation-delay: 2s;
  -webkit-animation-duration: 40s;
  animation-duration: 40s;
  background-color: rgba(255, 255, 255, 0.3);
}

.bg-bubbles li:nth-child(10) {
  left: 90%;
  width: 160px;
  height: 160px;
  -webkit-animation-delay: 11s;
  animation-delay: 11s;
}

@-webkit-keyframes square {
  0% {
    -webkit-transform: translateY(0);
    transform: translateY(0);
  }

  100% {
    -webkit-transform: translateY(-700px) rotate(600deg);
    transform: translateY(-700px) rotate(600deg);
  }
}

@keyframes square {
  0% {
    -webkit-transform: translateY(0);
    transform: translateY(0);
  }

  100% {
    -webkit-transform: translateY(-700px) rotate(600deg);
    transform: translateY(-700px) rotate(600deg);
  }
}
</style>